package com.misty.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.misty.admin.service.CommentService;
import com.misty.common.bean.Result;
import com.misty.common.constants.ResultEnum;
import com.misty.dao.domain.Comment;
import com.misty.dao.dto.CommentDto;
import com.misty.dao.mapper.CommentMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cthulhu
 * @since 2022-11-21
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    private CommentMapper commentMapper;

    /**
     *
     * @Param
     * @return
     * @Description 新增评论
     * @Date
     **/
    @Override
    public Result<?> saveComment(Comment comment) {
        comment.setCreateTime(LocalDateTime.now());
        comment.setUpdateTime(LocalDateTime.now());
        int insert = commentMapper.insert(comment);
        if(insert <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"新增评论失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"新增评论成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 更新评论
     * @Date
     **/
    @Override
    public Result<?> updateComment(Comment comment) {
        comment.setUpdateTime(LocalDateTime.now());
        int update = commentMapper.updateById(comment);
        if(update <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"更新评论失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"更新评论成功！");
    }


    /**
     *
     * @Param
     * @return
     * @Description 删除评论
     * @Date
     **/
    @Override
    public Result<?> deleteComment(Integer commentId) {
        int delete = commentMapper.deleteById(commentId);
        if(delete <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"删除评论失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"删除评论成功！");
    }

    /**
     *
     * @Param
     * @return
     * @Description 分页查询评论
     * @Date
     **/
    @Override
    public Result<?> Page(Integer pageNum, Integer pageSize, String search
    ) {
        LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = Wrappers.<Comment>lambdaQuery();
        if(StrUtil.isNotBlank(search)){
            commentLambdaQueryWrapper.like(Comment::getContent, search);
        }
        Page<CommentDto> commentPage = commentMapper.selectPageWithUser(new Page<>(pageNum, pageSize),commentLambdaQueryWrapper);
        if(commentPage.getTotal() <= 0){
            Result.error(ResultEnum.FAILURE.getValue(),"分页查询评论失败！");
        }
        return Result.success(ResultEnum.SUCCESS.getValue(),"分页查询评论成功！",commentPage);
    }

    @Override
    public Result<Map> statistics() {
        //获取今年的年数
        int year = LocalDateTime.now().getYear();

        //存储数据
        Map<String,Map> countMap = new HashMap<>();
        HashMap<Integer, Integer> commentCount = new HashMap<>();

        QueryWrapper<Comment> queryWrapper;
        for (int i =1; i <= 12; i++){
            String month = "";
            if(i < 10){
                month = "0" + i;
            }else {
                month = String.valueOf(i);
            }
            queryWrapper  = new QueryWrapper<>();
            queryWrapper  = new QueryWrapper<>();queryWrapper.eq("DATE_FORMAT (create_time,'%Y-%m')",year + "-" + month);
            Long blogMonthCount = commentMapper.selectCount(queryWrapper);
            commentCount.put(i, Math.toIntExact(blogMonthCount));
        }
        countMap.put("commentCount",commentCount);
        return Result.success("统计成功！",countMap);
    }
}
